/**
 * 
 */
package edu.jhu.tcj.purfuit.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import edu.jhu.tcj.purfuit.Category;
import edu.jhu.tcj.purfuit.Question;
import edu.jhu.tcj.purfuit.QuestionFactory;

/**
 * @author Bobby
 *
 */
public class ManageQuesView extends JPanel implements ActionListener,ListSelectionListener{
	private JPanel mainPanel;
	private JPanel questionInfo;
	private JButton exitButton;
	private JButton removeButton;
	private JButton addQuestion;
	private JButton saveQuestion;
	private JComboBox categories;
	
	private JTextField question;
	private JTextField correctAnswer;
	private JTextField incAnswer1;
	private JTextField incAnswer2;
	private JTextField incAnswer3;
	private JTextField incAnswer4;
	private JTextField incAnswer5;
	private JLabel message;
	
	private JList questionList;
	private JScrollPane questionPane;
	
	private Question[] questions;
	private Category[] cats;
	
	private JFrame frame;
	
	public ManageQuesView(Question[] questions, Category[] cats){
		this.questions = questions;
		this.sortQuestionsByID();
		
		this.cats = cats;
		mainPanel = new JPanel(new BorderLayout());
		questionInfo = new JPanel(new GridLayout(0,1));
		questionInfo.setBorder(new EmptyBorder(10,10,10,10));
		
		exitButton = new JButton("Exit");
		exitButton.setActionCommand("exit");
		exitButton.addActionListener(this);
		
		addQuestion = new JButton("Add New Question");
		addQuestion.setActionCommand("addnewquestion");
		addQuestion.addActionListener(this);
		
		removeButton = new JButton("Remove Question");
		removeButton.setActionCommand("removequestion");
		removeButton.addActionListener(this);
		
		saveQuestion = new JButton("Save Question");
		saveQuestion.setActionCommand("savequestion");
		saveQuestion.addActionListener(this);
		
		categories = new JComboBox(cats);
		
		question = new JTextField();
		correctAnswer = new JTextField();
		incAnswer1 = new JTextField();
		incAnswer2 = new JTextField();
		incAnswer3 = new JTextField();
		incAnswer4 = new JTextField();
		incAnswer5 = new JTextField();
		message = new JLabel();
		
		questionList = new JList(getQuestionList().toArray());
		questionList.setSelectedIndex(0);
		questionList.addListSelectionListener(this);
		questionPane = new JScrollPane(questionList);
		questionPane.setPreferredSize(new Dimension(100,100));
		
		if(questions.length > 0){
			setupQuestionInfo(0);
		}
		
		JLabel topLabel = new JLabel("Manage Questions");
		topLabel.setFont(new Font(null, Font.BOLD, 20));
		topLabel.setBorder(new EmptyBorder(5,5,5,5));
		mainPanel.add(topLabel, BorderLayout.NORTH);
		mainPanel.add(questionPane, BorderLayout.WEST);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(removeButton);
		buttonPanel.add(saveQuestion);
		buttonPanel.add(addQuestion);
		buttonPanel.add(exitButton);
		mainPanel.add(buttonPanel, BorderLayout.SOUTH);
		
		setupQuestionEdit();
		mainPanel.add(questionInfo, BorderLayout.CENTER);
		
		this.add(mainPanel);
		
		frame = new JFrame();
		frame.setContentPane(this);
		frame.setTitle("Trivial Purfuit - Manage Questions");
		frame.pack();
		frame.setVisible(true);
	}
	
	public List<Question> getQuestionList(){
		Vector newList = new Vector();
		for(int i = 0; i < questions.length; i++){
			newList.add(" Question " + questions[i].getID() + " ");
		}
		
		return newList;
	}
	
	public void setupQuestionEdit(){
		questionInfo.add(new JLabel("Question: "));
		questionInfo.add(question);
		questionInfo.add(new JLabel("Category: "));
		questionInfo.add(categories);
		questionInfo.add(new JLabel("Correct Answer: "));
		questionInfo.add(correctAnswer);
		questionInfo.add(new JLabel("Incorrect Answer: "));
		questionInfo.add(incAnswer1);
		questionInfo.add(new JLabel("Incorrect Answer: "));
		questionInfo.add(incAnswer2);
		questionInfo.add(new JLabel("Incorrect Answer: "));
		questionInfo.add(incAnswer3);
		questionInfo.add(new JLabel("Incorrect Answer: "));
		questionInfo.add(incAnswer4);
		questionInfo.add(new JLabel("Incorrect Answer: "));
		questionInfo.add(incAnswer5);
		questionInfo.add(message);
	}
	
	public void setupQuestionInfo(int entryID){
		Question ques = questions[entryID];
		question.setText(ques.getQuestion());
		correctAnswer.setText(ques.getCorrectAnswer());
		incAnswer1.setText("");
		incAnswer2.setText("");
		incAnswer3.setText("");
		incAnswer4.setText("");
		incAnswer5.setText("");
		if(ques.getIncorrectAnswers() != null){
			int length = ques.getIncorrectAnswers().length;
			if(length > 0)incAnswer1.setText(ques.getIncorrectAnswers()[0]);
			if(length > 1)incAnswer2.setText(ques.getIncorrectAnswers()[1]);
			if(length > 2)incAnswer3.setText(ques.getIncorrectAnswers()[2]);
			if(length > 3)incAnswer4.setText(ques.getIncorrectAnswers()[3]);
			if(length > 4)incAnswer5.setText(ques.getIncorrectAnswers()[4]);
		}
		if(ques.getCategory() == null) categories.setSelectedIndex(0);
		else categories.setSelectedIndex(ques.getCategory().getID() - 1);
		message.setText("");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("addnewquestion")){
			Question newQues = new Question();
			int id = questions.length + 1;
			newQues.setID(id);
			ArrayList<Question> quesList = new ArrayList();
			for(Question temp: questions){
				quesList.add(temp);
			}
			quesList.add(newQues);
			questions = new Question[quesList.size()];
			for(int i = 0; i < quesList.size(); i++){
				questions[i] = quesList.get(i);
			}
			questionList.setListData(getQuestionList().toArray());
			questionList.setSelectedIndex(questions.length - 1);
		}
		if(e.getActionCommand().equals("savequestion")){
			int numSelected = questionList.getSelectedIndex();
			ArrayList<String> incAnswers = new ArrayList();
			if(incAnswer1.getText() != null && !incAnswer1.getText().equals(""))
				incAnswers.add(incAnswer1.getText());
			if(incAnswer2.getText() != null && !incAnswer2.getText().equals("")) 
				incAnswers.add(incAnswer2.getText());
			if(incAnswer3.getText() != null && !incAnswer3.getText().equals("")) 
				incAnswers.add(incAnswer3.getText());
			if(incAnswer4.getText() != null && !incAnswer4.getText().equals("")) 
				incAnswers.add(incAnswer4.getText());
			if(incAnswer5.getText() != null && !incAnswer5.getText().equals("")) 
				incAnswers.add(incAnswer5.getText());
			
			if(incAnswers.size() < 3){
				message.setForeground(Color.RED);
				message.setText("You must enter at least three incorrect answers");
				return;
			}
			
			String[] incAnsArray = new String[incAnswers.size()];
			for(int i = 0; i < incAnswers.size(); i++){
				incAnsArray[i] = incAnswers.get(i);
			}
			if(questions[numSelected].getID() == (numSelected+1)){
				questions[numSelected].setQuestion(question.getText());
				questions[numSelected].setCorrectAnswer(correctAnswer.getText());

				questions[numSelected].setIncorrectAnswers(incAnsArray);
				
				Category cat = (Category)categories.getSelectedItem();
				questions[numSelected].setCategory(cat);
	
				
			}
			
			message.setForeground(Color.GREEN);
			message.setText("Question " + questions[numSelected].getID() + " saved!");
		}
		if(e.getActionCommand().equals("removequestion")){
			System.out.println("Pressed remove");
		}
		if(e.getActionCommand().equals("exit")){
			System.out.println("Pressed exit");
		}
		
	}
	
	public void sortQuestionsByID(){
		ArrayList<Question> quesList = new ArrayList();
		for(Question temp: questions){
			quesList.add(temp);
		}
		
		ArrayList<Question> newList = new ArrayList();
		while(!quesList.isEmpty()){
			Question lowest = null;
			for(Question sortQues: quesList){
				if(lowest == null) lowest = sortQues;
				if(sortQues.getID() < lowest.getID()){
					lowest = sortQues;
				}
			}
			newList.add(lowest);
			quesList.remove(lowest);
		}
		
		for(int i = 0; i < questions.length; i++){
			questions[i] = newList.get(i);
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		 if (e.getValueIsAdjusting() == false) {
	        if (questionList.getSelectedIndex() == -1) {

	        } else {
	            setupQuestionInfo(questionList.getSelectedIndex());
	        }
		 }
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/**Category cat1 = new Category();
		cat1.setName("People");
		cat1.setColor(new Color(255,255,255));
		Category cat2 = new Category();
		cat2.setName("Places");
		cat2.setColor(new Color(0,0,255));
		Category cat3 = new Category();
		cat3.setName("Events");
		cat3.setColor(new Color(0,255,0));
		Category cat4 = new Category();
		cat4.setName("Independence Day Holiday");
		cat4.setColor(new Color(255,0,0));
		
		Category[] cats = new Category[4];
		
		cats[0] = cat1;
		cats[1] = cat2;
		cats[2] = cat3;
		cats[3] = cat4;
		
		Question[] questions = new Question[2];
		
		Question ques1 = new Question();
		ques1.setCategory(cat1);
		ques1.setQuestion("When was the declaration of independence signed?");
		ques1.setCorrectAnswer("1776");
		ques1.setID(1);
		String[] inAnsw = new String[5];
		inAnsw[0] = "1779";
		inAnsw[1] = "1771";
		inAnsw[2] = "1773";
		inAnsw[3] = "1775";
		inAnsw[4] = "1777";
		ques1.setIncorrectAnswers(inAnsw);
		questions[0] = ques1;
		
		Question ques2 = new Question();
		ques2.setCategory(cat2);
		ques2.setQuestion("Who was first to sign the document?");
		ques2.setCorrectAnswer("John Hancock");
		ques2.setID(2);
		String[] inAnsw2 = new String[5];
		inAnsw2[0] = "Thomas Jefferson";
		inAnsw2[1] = "John Adams";
		inAnsw2[2] = "Samuel Adams";
		inAnsw2[3] = "Ben Franklin";
		inAnsw2[4] = "Bobby Ray";
		ques2.setIncorrectAnswers(inAnsw2);
		questions[1] = ques2;
		**/
		
		QuestionFactory factory = QuestionFactory.getInstance(new File("conf/Questions"));
		Map<Category, List<Question>> quesMap = factory.getAllQuestions();
		List<Question> quesList = new ArrayList<Question>();
		for (Map.Entry<Category, List<Question>> e : quesMap.entrySet()){
			quesList.addAll((List<Question>)e.getValue());
		}
		int size = quesList.size();
		Question[] questions = new Question[size];
		for(int i = 0; i < size; i++){
			questions[i] = quesList.get(i);
		}
		
		Category[] cats = null;
		try{
			cats = Category.loadCategories();
		}catch(Exception e){
			e.printStackTrace();
		}
		ManageQuesView view = new ManageQuesView(questions, cats);
		
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(view);
		frame.setSize(550,575);
		frame.setVisible(true);
	}

}
